package net.airtheva.uniqueTask2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.widget.ImageView;

public class Utils {

	static String ReadTextFromStream(InputStream is) {
		
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
			StringBuilder sb = new StringBuilder("");
			String line = null;
			while((line = br.readLine()) != null) {
				sb.append(line);
				sb.append("\n");
			}
			return sb.toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	static void SaveToFile(String path, String content) {
		
		try {
			FileWriter fw = new FileWriter(path, false);
			fw.write(content);
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	// Copy from http://www.cnblogs.com/bobli/archive/2012/03/26/2418694.html.
	static String MD5(String content) {
		
		try {
			
			byte[] hash;
			hash = MessageDigest.getInstance("MD5").digest(content.getBytes("UTF-8"));
			StringBuilder hex = new StringBuilder(hash.length * 2);
		    for (byte b : hash) {
		        if ((b & 0xFF) < 0x10) hex.append("0");
		        hex.append(Integer.toHexString(b & 0xFF));
		    }
		    return hex.toString();
		    
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "";
		
	}
	
	public static String CacheIconImage(Context context, String uri) {
		
			try {
				File cache = new File(context.getCacheDir(), "i" + MD5(uri));
				if(!cache.exists()) {
					BitmapFactory.Options o = new BitmapFactory.Options();
					o.outWidth = 64;
					o.outHeight = 64;
					o.inPreferredConfig = Bitmap.Config.RGB_565;
					Bitmap b = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(Uri.parse(uri)), null, o);
					FileOutputStream fos = new FileOutputStream(cache);
					b.compress(Bitmap.CompressFormat.JPEG, 90, fos);
					b.recycle();
					fos.close();
				}
				return "file://" + cache.getAbsolutePath();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return "";
	}
	
	public static String CacheBackgroundImage(Context context, String uri) {
		
		try {
			File cache = new File(context.getCacheDir(), "b" + MD5(uri));
			if(!cache.exists()) {
				BitmapFactory.Options o = new BitmapFactory.Options();
				o.outWidth = 480;
				o.outHeight = 480;
				o.inPreferredConfig = Bitmap.Config.RGB_565;
				Bitmap b = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(Uri.parse(uri)), null, o);
				FileOutputStream fos = new FileOutputStream(cache);
				b.compress(Bitmap.CompressFormat.JPEG, 90, fos);
				b.recycle();
				fos.close();
			}
			return "file://" + cache.getAbsolutePath();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "";
	}

	public static String CacheEmbeddedImage(Context context, String uri) {
		try {
			File cache = new File(context.getCacheDir(), "e" + MD5(uri));
			if(!cache.exists()) {
				BitmapFactory.Options o = new BitmapFactory.Options();
				
				o.inJustDecodeBounds = true;
				BitmapFactory.decodeStream(context.getContentResolver().openInputStream(Uri.parse(uri)), null, o);
				
				o.inJustDecodeBounds = false;
				if(o.outWidth > 120) {
					o.inSampleSize = o.outWidth / 120;
				}
				if(o.outHeight > 120) {
					o.inSampleSize = o.outHeight / 120;
				}
				o.inPreferredConfig = Bitmap.Config.RGB_565;
				Bitmap b = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(Uri.parse(uri)), null, o);
				
				FileOutputStream fos = new FileOutputStream(cache);
				b.compress(Bitmap.CompressFormat.JPEG, 90, fos);
				b.recycle();
				fos.close();
			}
			return "file://" + cache.getAbsolutePath();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "";
	}

}
